<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="strutsTags"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<html>
	<base href="<%=basePath%>">
	<head>
		<link href="resource/css/style.css" rel="stylesheet" type="text/css" />
		<title><s:text name="desc.add" /> <s:text
				name="desc.security.user" />
		</title>
	</head>
	<body>
		<s:component template="bodyhead">
			<s:param name="pagetitle" value="'新增用户：'" />
		</s:component>

		<s:form id="addForm" name="addForm" method="post" action="addUser"
			namespace="/configuration">
			<s:hidden id="roleIds" name="roleIds" />
			<s:hidden id="status" name="user.uiLevelRestrict" />
			<s:hidden id="macLimit" name="user.uiMacRestrict" />
			<table width="100%" class="formtable">
				<tr>
					<s:hidden id="empIds" name="empIds" />
					<td nowrap="nowrap" align="left" colspan="6">
						请选择员工(单个新增可指定用户名或密码)
						<font color="red">*</font>:
						<s:textfield id="empNames" name="empNames" readonly="true"
							size="30" />
						<img src="resource/images/search_icon.gif" id="search_emp"
							style="CURSOR: pointer;"
							onclick="empSelector_searchEmp();hrm.common.openDialog('dlgSelectEmp');"
							alt='点击图标选择员工' />
					</td>
				</tr>

				<tr align="left" id="nameandpassword" style="display: none">
					<td>
						<table>
							<tr>
								<s:textfield id="user_name" label="用户名" name="user.uiUsername"
									maxlength="64" size="16" />
								<s:password id="uiPassword" name="user.uiPassword" label="密码"
									onkeyup="isPassword(this);" maxlength="32" size="16" />
								<s:password id="confirmPassword" name="confirmPassword"
									label="重复密码" onkeyup="isPassword(this);" maxlength="32"
									size="16" />
							</tr>
						</table>
					</td>
				</tr>

				<tr>
					<td>
						<br />
						<s:text name="desc.security.userhasrole" />
						(
						<s:text name="desc.security.multiple" />
						)
					</td>
				</tr>

				<s:if test="roleList!=null">
					<tr>
						<td>
							<table width="80%">
								<tr>
									<s:iterator value="roleList" status="index">
										<td>
											<input class="checkbox" type="checkbox" id="roleId"
												name="roleId" <s:if test="#index.first==true">checked</s:if>
												value="<s:property value="roleNo"/>" />
											<s:property value="roleName" />
										</td>
										<s:if test="#index.modulus(4)==0 || #index.last==true">
								</tr>
								<tr>
									</s:if>
									</s:iterator>
								</tr>
							</table>
						</td>
					</tr>
				</s:if>

				<tr>
					<td align="left">
						登录限制:
						<br />
						<input type="radio" class="checkbox" name="user_uiLevelRestrict0"
							id="user_uiLevelRestrict0" value="0"
							onclick="selectIPorMAC(this);">
						无限制
						<input type="radio" class="checkbox" name="user_uiLevelRestrict1"
							id="user_uiLevelRestrict1" value="1"
							onclick="selectIPorMAC(this);">
						限制到IP
						<input type="radio" class="checkbox" name="user_uiLevelRestrict2"
							id="user_uiLevelRestrict2" value="2"
							onclick="selectIPorMAC(this);">
						限制到机器
						<input type="radio" class="checkbox" name="user_uiLevelRestrict3"
							id="user_uiLevelRestrict3" value="3"
							onclick="selectIPorMAC(this);">
						限制到机器或IP
					</td>
				</tr>

				<tr>
					<td align="left">
						允许访问的IP地址(段):
						<s:textfield id="user_uiIpRestrict" name="user.uiIpRestrict"
							maxlength="64" required="false" />
						如:192.168.0.1-192.168.0.118 或者 192.168.0.*
					</td>
				</tr>
			</table>
			<table height="40px" align="center">
				<tr>
					<td>
						<input title="[Alt+S]" accesskey="S" id="submited" name="submited"
							class="button" type="button" onclick="checkAddUser();" value="确定">
						<input title="[Alt+A]" accesskey="A" id="reset" name="reset"
							class="button" type="button" onclick="returnBack();" value="取消">
					</td>
				</tr>
			</table>
		</s:form>

		<div id="dlgSelectEmp" style="display: none;">
			<div id="mulSelect">
				<input type="hidden" id="empStatus" value="0" />
				<s:empselector id="empselector1" name="emp.empDeptNo.departmentName"
					condition="empNotUser" hiddenFieldName="emp.empDeptNo.id" />
			</div>
			<table width="100%">
				<tr>
					<td align="center">
						<input class="button" type="button" value="确定" onclick="empAdd();" />
						<input class="button" type="button" value="取消"
							onclick="hrm.common.closeDialog('dlgSelectEmp');" />
					</td>
				</tr>
			</table>
		</div>

		<script type="text/javascript" language="javascript">
	hrm.common.initDialog('dlgSelectEmp', 560);
	$("#status").val(0);
	$("#user_uiIpRestrict").attr('disabled', true);
	$("#user_uiLevelRestrict0").attr('checked', true);
	$("#user_uiLevelRestrict1").attr('checked', false);
	$("#user_uiLevelRestrict2").attr('checked', false);
	$("#user_uiLevelRestrict3").attr('checked', false);
	// 设置访问限制；
	function selectIPorMAC(obj) {
		var level = obj.value;
		if (level == 0) {
			$("#user_uiIpRestrict").attr('disabled', true);
			$("#user_uiLevelRestrict1").attr('checked', false);
			$("#user_uiLevelRestrict2").attr('checked', false);
			$("#user_uiLevelRestrict3").attr('checked', false);
			$("#status").val(0);
		} else if (level == 1) {
			$("#user_uiIpRestrict").attr('disabled', false);
			$("#user_uiLevelRestrict0").attr('checked', false);
			$("#user_uiLevelRestrict2").attr('checked', false);
			$("#user_uiLevelRestrict3").attr('checked', false);
			$("#status").val(1);
		} else if (level == 2) {
			$("#user_uiIpRestrict").attr('disabled', true);
			$("#user_uiLevelRestrict1").attr('checked', false);
			$("#user_uiLevelRestrict0").attr('checked', false);
			$("#user_uiLevelRestrict3").attr('checked', false);
			$("#macLimit").val('00-00-00-00-00-00');
			$("#status").val(2);
		} else if (level == 3) {
			$("#user_uiIpRestrict").attr('disabled', false);
			$("#user_uiLevelRestrict1").attr('checked', false);
			$("#user_uiLevelRestrict0").attr('checked', false);
			$("#user_uiLevelRestrict2").attr('checked', false);
			$("#macLimit").val('00-00-00-00-00-00');
			$("#status").val(3);
		}
	}

	// 检查要添加的用户；
	function checkAddUser() {
		var str2 = document.getElementById("empIds").value;
		if (str2 == "") {
			alert("请至少选择一个员工！");
			return;
		}

		// 如果用户输入了密码，检查两次输入密码是否一致；
		var newPassword = $("#uiPassword").val();
		var newPassword2 = $("#confirmPassword").val();
		if (newPassword.trim().length > 0 && newPassword.length < 4) {
			hrm.common.appendEnd('uiPassword', '至少4位！');
			return;
		}
		if ((newPassword.trim().length > 0 || newPassword2.trim().length > 0)
				&& newPassword != newPassword2) {
			hrm.common.appendEnd('uiPassword', '');
			hrm.common.appendEnd('confirmPassword', '密码不一致！');
			return;
		} else {
			hrm.common.appendEnd('confirmPassword', '');
		}

		// 检查角色选择；
		var allRoles = document.getElementsByName('roleId');
		var flag = -1;
		var testRoles = "";
		for (i = 0; i < allRoles.length; i++) {
			if (allRoles[i].checked == true)
				flag = i;
			if (allRoles[i].checked == true)
				testRoles += allRoles[i].value + ',';
			else
				testRoles += '0,';
		}
		if (flag == -1) {
			alert("必须选择一个角色！");
			return;
		}
		if (testRoles.length > 1) {
			testRoles = testRoles.substring(0, testRoles.length - 1);
		}
		$('#roleIds').val(testRoles);

		// 检查登录限制；
		if ($("#status").val() == 1) {
			if ($("#user_uiIpRestrict").val() == "") {
				alert("请输入限制的IP地址(段)！");
				return;
			} else if (!hrm.common.checkIP($("#user_uiIpRestrict").val())) {
				return;
			}
		}

		if (!confirm('您确定要添加所选用户吗？'))
			return;
		document.getElementById('submited').disabled = true;
		document.getElementById('addForm').submit();
	}

	// 选择员工后的检查；
	function empAdd() {
		var count = empSelector_choseEmpCount();
		var selectedName = empSelector_choseEmpNameStr();
		var selectedId = empSelector_choseEmpIdStr();
		if (count == 0) {
			alert("请至少选择一名员工！");
			return;
		} else if (count == 1) {
			document.getElementById("nameandpassword").style.display = "inline";

			// 设置默认用户名为员工编号；
			var selectEmpNo = empSelector_choseEmpDistinctNoStr();
			document.getElementById("user_name").value = selectEmpNo;

			// 设置默认密码，随机生成；
			var password = hrm.common.randomStr(6, true, true, true);
			$("#uiPassword").val(password);
			$("#confirmPassword").val(password);
		} else {
			document.getElementById("user_name").value = "";
			document.getElementById("uiPassword").value = "";
			document.getElementById("confirmPassword").value = "";
			document.getElementById("nameandpassword").style.display = "none";
		}

		$("#empNames").val(selectedName);
		$("#empIds").val(selectedId);
		hrm.common.closeDialog('dlgSelectEmp');
	}

	document.getElementById("uiPassword").value = '<s:property value="user.uiPassword"/>';
	document.getElementById("confirmPassword").value = '<s:property value="user.uiPassword"/>';
	var empIdArr = $("#empIds").val().split(";");
	if ($("#empIds").val() != "" && empIdArr.length == 1)
		document.getElementById("nameandpassword").style.display = "inline";

	function returnBack() {
		window.location = "userList.action";
	}
</script>
	</body>
</html>
